package top.hzy520.search.service;

import io.searchbox.client.http.JestHttpClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.hzy520.search.entity.PageSearch;
import top.hzy520.search.entity.Product;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 12:38 2018/6/20
 */
@Service
public class SearchServiceImpl  implements SearchService{
    int PAGE_SIZE=10;//默认分页大小
    int PAGE_NUMBER=0; //默认当前分页数
    @Autowired
    private JestHttpClient esClient;
    @Override
    public PageSearch search(PageSearch pageSearch) throws IOException {
        //判断是否输入页数和每页size如果没有使用默认
        if (pageSearch.getPageSize()==0){
            pageSearch.setPageSize(PAGE_SIZE);
        }
        if (pageSearch.getPageNumber()==0){
            pageSearch.setPageNumber(PAGE_NUMBER);
        }
         //创建es分页查询
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
           searchSourceBuilder.query(QueryBuilders.boolQuery()
                   .must(QueryBuilders.matchQuery("spuName",pageSearch.getSearchContent()))
                   .must(QueryBuilders.matchQuery("status",1)))
                   .from((pageSearch.getPageNumber()-1)*pageSearch.getPageSize())
                   .size(pageSearch.getPageSize());
//        searchSourceBuilder.highlight()
//                .field("spu_name")
//                .preTags("<em>").postTags("</em>")
//                .fragmentSize(200);
        Search search=new Search.Builder(searchSourceBuilder.toString())
//                .addIndex("yuyubuygoods")
                .build();
        SearchResult result=esClient.execute(search);
        //将查询出来的数据转换成product
        List<Product> productList=new ArrayList<>();
           result.getHits(Product.class).stream().forEach(param->{
               Product product=param.source;
               productList.add(product);
             });
           pageSearch.setProductList(productList);
           pageSearch.setTotalSize(result.getTotal());
        return pageSearch;
    }
   /**
   *@Author: HouZeYu
   *@Description: 通过子分类查询商品
   *@Date: 23:12 2018/6/23
   */
    @Override
    public PageSearch searchProductByCategoryId(PageSearch pageSearch) throws IOException {
        //判断是否输入页数和每页size如果没有使用默认
        if (pageSearch.getPageSize()==0){
            pageSearch.setPageSize(PAGE_SIZE);
        }
        if (pageSearch.getPageNumber()==0){
            pageSearch.setPageNumber(PAGE_NUMBER);
        }
        //创建es查询
        SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("categoryId",pageSearch.getSearchContent()))
                .must(QueryBuilders.matchQuery("status",1)))
                .from((pageSearch.getPageNumber()-1)*pageSearch.getPageSize())
                .size(pageSearch.getPageSize());
        Search search=new Search.Builder(sourceBuilder.toString()).build();
        //调用es客户端执行查询
        SearchResult result=esClient.execute(search);
        //将查出来数据转换成product
        List<Product> productList=new ArrayList<>();
        result.getHits(Product.class).stream().forEach(param->{
            Product product=param.source;
            productList.add(product);
        });
         pageSearch.setProductList(productList);
         pageSearch.setTotalSize(result.getTotal());
        return pageSearch;
    }
        /**
        *@Author: HouZeYu
        *@Description: 通过父分类查询商品
        *@Date: 23:46 2018/6/23
        */
    @Override
    public PageSearch searchProductByCategoryParentId(PageSearch pageSearch) throws IOException {
        //判断是否输入页数和每页size如果没有使用默认
        if (pageSearch.getPageSize()==0){
            pageSearch.setPageSize(PAGE_SIZE);
        }
        if (pageSearch.getPageNumber()==0){
            pageSearch.setPageNumber(PAGE_NUMBER);
        }
        //创建es查询
        SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("categoryParentId",pageSearch.getSearchContent()))
                .must(QueryBuilders.matchQuery("status",1)))
                .from((pageSearch.getPageNumber()-1)*pageSearch.getPageSize())
                .size(pageSearch.getPageSize());
        Search search=new Search.Builder(sourceBuilder.toString()).build();
        //调用es客户端执行查询
        SearchResult result=esClient.execute(search);
        //将查出来数据转换成product
        List<Product> productList=new ArrayList<>();
        result.getTotal();
        result.getHits(Product.class).stream().forEach(param->{
            Product product=param.source;
            productList.add(product);
        });
        pageSearch.setProductList(productList);
        pageSearch.setTotalSize(result.getTotal());
        return pageSearch;
    }
}
